package com.yty.controller;

import com.github.pagehelper.Page;
import com.yty.annotation.LogOperation;
import com.yty.page.PageResult;
import com.yty.param.StudentQueryParam;
import com.yty.pojo.Result;
import com.yty.pojo.Student;
import com.yty.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result findStudent(StudentQueryParam param){
        log.info("查询学生列表，参数：{}",param);
        PageResult students = studentService.findStudent(param);
        log.info("查询学生列表成功:{}", students );
        return Result.success(students);
    }
    @GetMapping("/{id}")
    public Result findStudentById(@PathVariable("id") Integer id){
        log.info("查询学生，参数：{}",id);
        Student student = studentService.findStudentById(id);
        log.info("查询学生成功:{}", student);
        return Result.success(student);
    }
    @LogOperation
    @PostMapping
    public Result addStudent(@RequestBody Student student){
        log.info("添加学生，参数：{}",student);
        Integer count = studentService.addStudent(student);
        if (count == 0)return Result.error("添加失败");
        log.info("添加学生成功");
        return Result.success();
    }
    @LogOperation
    @DeleteMapping("/{ids}")
    public Result deleteStudentById(@PathVariable("ids") Integer... ids){
        log.info("删除学生，id:{}",ids);
        Integer count = studentService.deleteStudentById(ids);
        if (count == 0)return Result.error("删除失败");
        log.info("删除学生成功");
        return Result.success();
    }
    @LogOperation
    @PutMapping
    public Result updateStudent(@RequestBody Student student){
        log.info("修改学生，参数：{}",student);
        Integer count = studentService.updateStudent(student);
        if (count == 0)return Result.error("修改失败");
        log.info("修改学生成功");
        return Result.success();
    }

    @LogOperation
    @PutMapping("/violation/{id}/{score}")
    public Result updateStudentViolation(@PathVariable("id") Integer id,@PathVariable("score") Integer score){
        log.info("修改学生违纪分数，参数：{},{}",id,score);
        Integer count = studentService.updateStudentViolation(id,score);
        if (count == -1)return Result.error("分数参数异常！");
        if (count == 0)return Result.error("修改失败！");
        log.info("修改学生违纪分数成功");
        return Result.success();
    }

}
